package cn.iocoder.yudao.module.common.controller.admin.grade;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.grade.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.grade.GradeDO;
import cn.iocoder.yudao.module.common.service.grade.GradeService;

@Tag(name = "管理后台 - 年级")
@RestController
@RequestMapping("/common/grade")
@Validated
public class GradeController {

    @Resource
    private GradeService gradeService;

    @PostMapping("/create")
    @Operation(summary = "创建年级")
    @PreAuthorize("@ss.hasPermission('common:grade:create')")
    public CommonResult<Long> createGrade(@Valid @RequestBody GradeSaveReqVO createReqVO) {
        return success(gradeService.createGrade(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新年级")
    @PreAuthorize("@ss.hasPermission('common:grade:update')")
    public CommonResult<Boolean> updateGrade(@Valid @RequestBody GradeSaveReqVO updateReqVO) {
        gradeService.updateGrade(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除年级")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:grade:delete')")
    public CommonResult<Boolean> deleteGrade(@RequestParam("id") Long id) {
        gradeService.deleteGrade(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得年级")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:grade:query')")
    public CommonResult<GradeRespVO> getGrade(@RequestParam("id") Long id) {
        GradeDO grade = gradeService.getGrade(id);
        return success(BeanUtils.toBean(grade, GradeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得年级分页")
    @PreAuthorize("@ss.hasPermission('common:grade:query')")
    public CommonResult<PageResult<GradeRespVO>> getGradePage(@Valid GradePageReqVO pageReqVO) {
        PageResult<GradeDO> pageResult = gradeService.getGradePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, GradeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出年级 Excel")
    @PreAuthorize("@ss.hasPermission('common:grade:export')")
    @OperateLog(type = EXPORT)
    public void exportGradeExcel(@Valid GradePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<GradeDO> list = gradeService.getGradePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "年级.xls", "数据", GradeRespVO.class,
                        BeanUtils.toBean(list, GradeRespVO.class));
    }

}